const login = require('../../login/index')
const caseDetail = require('../../component/caseDetail')
const ajax = require('../../../tool/ajax')
const searchForArbitrators = require('../../component/searchForArbitrators')
let page, config, user, caseInfo;

/**
 * 选择仲裁员
 */
const start = async (pupConfig, myCaseInfo) => {
    page = pupConfig.page;
    config = pupConfig.config.common;
    caseInfo = myCaseInfo;
    await step1(pupConfig);
    await step2(pupConfig);
}

/**
 * 步骤一：当事人选仲裁员
 */
const step1 = async (pupConfig) => {
    let { respondent, applicant } = pupConfig.config.tribunal;
    //被申请人选仲裁员
    await login(pupConfig, pupConfig.config.filing.respondent1.id)
    await selectArbitrators(pupConfig, respondent);
    await login(pupConfig);
    //申请人选仲裁员
    await selectArbitrators(pupConfig, applicant);
}

/**
 * 步骤二：秘书确定组庭方式和审理方式
 */
const step2 = async (pupConfig) => {
    await login(pupConfig, caseInfo.secretaryId)
    await comfirmWays(pupConfig, pupConfig.config.tribunal.secretary)
}

/**
 * 当事人选择仲裁员
 * @param pupConfig 基础配置
 * @param party 当事人配置
 */
const selectArbitrators = async (pupConfig, party) => {
    await courtyardStyle(pupConfig, party);//组庭方式和审理方式
    // TODO 待完善1
    await selectSingle(pupConfig, party);
    await selectLinesman(pupConfig, party);
    await selectChief(pupConfig, party);
    await nextStep(page);
}

/**
 * 当事人选择独任制仲裁员
 * @param pupConfig 基础配置
 * @param party 当事人配置
 */
const selectSingle = async (pupConfig, party) => {
    user = await page.$eval('.zc-pubilc-header-user span', el => el.innerText);//用户名
    await page.waitFor('.chooseWay_select_box');//选择独任制与合议制的仲裁员 页面加载
    let singles = await page.$$('.chooseWay_select_box .ant-radio-group .ant-radio-wrapper');//选择独任仲裁员
    await singles[party.single].click();

    if (party.single === 0) {//自主选择
        await chooseBySelf(pupConfig, party.chiefId);
        await page.waitFor('.chooseWay_select_box');
    }
}

/**
 * 当事人选择边裁仲裁员
 * @param pupConfig 基础配置
 * @param party 当事人配置
 */
const selectLinesman = async (pupConfig, party) => {
    await page.waitFor('.chooseWay_select_box');//选择独任制与合议制的仲裁员 页面加载
    let singles = await page.$$('.chooseWay_select_box .ant-radio-group .ant-radio-wrapper');//选择独任仲裁员
    //选择边裁仲裁员
    await singles[party.linesman].click();
    if (party.linesman === 3) {//自主选择
        await chooseBySelf(pupConfig, party.linesmanId);
        await page.waitFor('.chooseWay_select_box');
    }
}

/**
 * 当事人选择首席仲裁员
 * @param pupConfig 基础配置
 * @param party 当事人配置
 */
const selectChief = async (pupConfig, party) => {
    await page.waitFor('.chooseWay_select_box');//选择独任制与合议制的仲裁员 页面加载
    let singles = await page.$$('.chooseWay_select_box .ant-radio-group .ant-radio-wrapper');//选择独任仲裁员
    //选择首席仲裁员
    await singles[party.chief].click();
    if (party.chief === 6) {//自主选择
        await chooseBySelf(pupConfig, party.chiefId);
        await page.waitFor('.chooseWay_select_box');
    }
}

/**
 * 当事人选择组庭方式和审理方式
 */
const courtyardStyle = async (pupConfig, Data) => {
    await caseDetail(pupConfig, caseInfo.caseId, 0);

    user = await page.$eval('.zc-pubilc-header-user span', el => el.innerText);
    await ajax.waitForResponse(pupConfig.page, 'confirmArbitrationRules.do');
    await page.waitFor('.chooseWay .way_main');
    //选择组庭方式
    let lables = await page.$$('.chooseWay .way_main div .ant-radio-wrapper');
    await lables[Data.courtyardStyle].click();
    //审理方式
    let ways = await page.$$('.chooseWay .way_not div label');
    await ways[Data.hearWays].click();

    let nextStep = await page.$$('.chooseWay .chooseWay_btn');
    //截屏
    await screen(page, `${user} 组庭方式`);
    await nextStep[0].click();
}
/**
 下一步
 */
const nextStep = async (page) => {
    let chooseWay_btn = await page.$$('.rs-page-route .chooseWay .chooseWay_btn');
    await chooseWay_btn[0].click();
    await page.waitFor(1000);//等待弹出框加载完成
    await page.waitFor('.ant-modal-content');
    let comfirm = await page.$$('.ant-modal-content .ant-modal-footer div button.ant-btn');
    await comfirm[1].click();
    //返回案件详情
    await page.waitFor('.comeback');
    await page.click('.comeback')
    //页面加载完成
    await page.waitFor('.zc-detail-status-content');
    await screen(`${user}选的仲裁员`)
}

/**
 * 自主选择仲裁员
 */
const chooseBySelf = async (pupConfig, Data) => {
    //选择配置文件的仲裁员名字
    await page.waitFor('button.chooseWay_btn2');
    let btnChoose = await page.$$('button.chooseWay_btn2 span')
    console.log(btnChoose.length)
    await btnChoose[0].click()
    await searchForArbitrators(pupConfig, Data)
    //确认
    let chiefBtn = await page.$$('.selectingAibitratorWidget-1-btn button')
    await chiefBtn[1].click();
    //弹出框加载
    await page.waitFor(1000);
    await page.waitFor('.ant-modal-footer');
    await page.click('.ant-modal-footer button.ant-btn-primary');
}

/**
 * 自主选择仲裁员:选择单个
 * @param pupConfig 基础配置
 * @param list 仲裁员列表
 * @param i list中的索引
 */
const chooseBySelfItem = async (pupConfig, list, i) => {
    /**
     * 搜索仲裁员名字
     */
    await page.$eval(".workYeah-font .ant-input", (selector) => selector.value = '');
    await page.type('.workYeah-font .ant-input', list[i].name)
    await page.click('.sureAndResetting button.sure')

    await ajax.waitForResponse(page, 'arbitratorRecommend.do');
    await page.waitFor('button.selectingAibitratorWidget-1-query-add span');
    await page.click('button.selectingAibitratorWidget-1-query-add span');
}

/**
 * 秘书确认组庭方式审理方式
 */
const comfirmWays = async (pupConfig, secretary) => {
    await caseDetail(pupConfig, caseInfo.caseId, 0);
    await ajax.waitForResponse(page, 'queryComposeAndCourt.do');
    await page.waitFor('.way');
    //组庭方式确认
    let comfirmLable = await page.$$('.way .way_main .ant-radio-group .ant-radio-wrapper');
    await comfirmLable[secretary.courtyardStyle].click()

    //审理方式确认
    await comfirmLable[secretary.hearWays].click()
    await screen('秘书确认组庭方式审理方式')
    await page.click('.way button.way_submit')
    await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');

}

/**
 * 截屏
 */
const screen = async (name = '案件关联') => {
    await page.screenshot({ path: config.imgPath + 'tribunal/' + name + '.png' });
}

module.exports = start;
